/*
 * Windy - Level Editor for Isometric Tile-based Games
 * Copyright (C) 2009 Ricardo J. Ricken (Darkl0rd)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef WINDY_FRAME_HPP
#define WINDY_FRAME_HPP

#include <wx/wxprec.h>

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include "constants.hpp"
//#include "windy_prefs.hpp"
#include "windy_document.hpp"
#include "isometric_grid.hpp"

#include <wx/imaglist.h>
#include <wx/log.h> 
#include <vector>

class TilePanel;

/**
* Implements the main window frame. Creates and maintains the user interface,
* interacting and responding to the user's actions.
*/
class WindyFrame : public wxFrame
{
public:
   WindyFrame (const wxString &title/*, MonkeyPrefs &mprefs*/, const wxPoint &pos = wxDefaultPosition, const wxSize &size = wxDefaultSize);
   virtual ~WindyFrame ();

   void OnFileNew (wxCommandEvent &);
   void OnFileClose (wxCommandEvent &);
   void OnHelpAbout (wxCommandEvent &);
   void OnPaint (wxPaintEvent &);
   void OnEraseBackground (wxEraseEvent &);
   void OnClose (wxCloseEvent &);
   void OnSize (wxSizeEvent &);
   void OnUpdateUI (wxUpdateUIEvent &);
   void OnMotion (wxMouseEvent &);
   void OnLeftDown (wxMouseEvent &);
   void OnLeaveWindow (wxMouseEvent &);

   void OnAddTileset (wxCommandEvent &);
   void OnChangeTileset (wxCommandEvent &);

#ifdef _DEBUG

   void OnDebugLogWindow (wxCommandEvent &);
   void OnDebugGridRegions (wxCommandEvent &);

#endif //~_DEBUG

   /**
   * Shows a simple message box.
   * @param message content
   * @param caption optional caption
   */
   inline void ShowWarning (const wxString &message, const wxString &caption = _("Windy - Error")) {
      wxMessageBox(message, caption, wxOK | wxICON_INFORMATION, this);
   }

private:
   void DoNewDocument (WindyDocument *newDoc);
   void DoCloseDocument ();
   void InitGridMap ();

   /**
   * Finds the widget represented by some given id and return it
   * properly casted to the type specified in the template.
   * @param id widget's identifier
   * @return Pointer to the desired widget, properly casted.
   */
   template <class T> inline T GetWindow (const long id) const {
      return dynamic_cast <T> (FindWindowById(id));
   }

   inline wxString getBaseTitle () const {
      return wxString::Format(wxT("Windy %s"), WDY_VERSION);
   }

   wxPoint MousePosToGridCol (const wxPoint &pos) {
      return wxPoint(pos.x / 16, pos.y / 8);
   }

   wxPoint MousePosToGridPos (const wxPoint &pos) {
      return wxPoint(pos.x % 16, pos.y % 8);
   }

   bool CursorOnBottom (const wxPoint &pos, int orient)
   {
      if (orient == GridRect_Down)
         return pos.x > pos.y * 2 ? false : true;
      else
         return pos.x > 15 - (pos.y * 2 + 1) ? true : false;
   }

   // current document
   bool is_doc_open;
   WindyDocument *doc;
   IsometricGrid *grid;

   // graphics
   wxImageList icons;
   wxImageList grid_tiles;
   wxBitmap *buffer;
   //MonkeyPrefs &prefs;

#ifdef _DEBUG
   // debug
   wxPoint over_grid_col;
   bool show_grid_regions;
#endif

   wxLogWindow *logger;
   TilePanel *tp;

   DECLARE_EVENT_TABLE();
};

#endif //~WINDY_FRAME_HPP
